// 圣经问答游戏 - MP3音频系统

class MP3AudioSystem {
    constructor() {
        this.enabled = true;
        this.volume = 0.5;
        this.audioElements = {};
        this.createAudioElements();
    }

    // 创建音频元素
    createAudioElements() {
        const sounds = {
            correct: 'assets/sounds/correct.mp3',
            wrong: 'assets/sounds/wrong.mp3',
            click: 'assets/sounds/click.mp3'
        };

        Object.keys(sounds).forEach(key => {
            const audio = new Audio();
            audio.src = sounds[key];
            audio.preload = 'auto';
            audio.volume = this.volume;
            this.audioElements[key] = audio;
        });

        // 创建背景音乐
        this.backgroundMusic = new Audio();
        this.backgroundMusic.src = 'assets/sounds/background.mp3';
        this.backgroundMusic.loop = true;
        this.backgroundMusic.volume = 0.2; // 背景音乐音量较低
        this.backgroundMusic.preload = 'auto';
        this.musicEnabled = true;
    }

    // 播放音效
    playSound(soundName) {
        if (!this.enabled || !this.audioElements[soundName]) return;
        
        try {
            const audio = this.audioElements[soundName];
            audio.volume = this.volume;
            audio.currentTime = 0;
            
            // 创建播放Promise来处理自动播放策略
            const playPromise = audio.play();
            if (playPromise !== undefined) {
                playPromise.catch(error => {
                    console.log('音效播放被阻止:', error);
                    // 在用户交互后重试
                    document.addEventListener('click', () => {
                        audio.play().catch(() => {});
                    }, { once: true });
                });
            }
        } catch (error) {
            console.log('音效播放失败:', error);
        }
    }

    // 设置音量
    setVolume(volume) {
        this.volume = Math.max(0, Math.min(1, volume));
        Object.values(this.audioElements).forEach(audio => {
            audio.volume = this.volume;
        });
    }

    // 启用/禁用音效
    setEnabled(enabled) {
        this.enabled = enabled;
        if (!enabled && this.backgroundMusic) {
            this.stopBackgroundMusic();
        }
    }

    // 播放背景音乐
    playBackgroundMusic() {
        if (this.backgroundMusic && this.musicEnabled && this.enabled) {
            this.backgroundMusic.play().catch(error => {
                console.log('背景音乐播放失败:', error);
            });
        }
    }

    // 停止背景音乐
    stopBackgroundMusic() {
        if (this.backgroundMusic) {
            this.backgroundMusic.pause();
            this.backgroundMusic.currentTime = 0;
        }
    }

    // 切换背景音乐
    toggleBackgroundMusic() {
        this.musicEnabled = !this.musicEnabled;
        if (this.musicEnabled) {
            this.playBackgroundMusic();
        } else {
            this.stopBackgroundMusic();
        }
        return this.musicEnabled;
    }

    // 设置背景音乐音量
    setMusicVolume(volume) {
        if (this.backgroundMusic) {
            this.backgroundMusic.volume = Math.max(0, Math.min(1, volume));
        }
    }

    // 检查音频支持
    isSupported() {
        return !!window.Audio;
    }

    // 预加载音效
    preloadSounds() {
        Object.values(this.audioElements).forEach(audio => {
            audio.load();
        });
    }

    // 用户交互后启用音频
    enableAudioAfterInteraction() {
        // 播放一个静音音频来启用音频上下文
        Object.values(this.audioElements).forEach(audio => {
            const originalVolume = audio.volume;
            audio.volume = 0;
            const playPromise = audio.play();
            if (playPromise !== undefined) {
                playPromise.then(() => {
                    audio.pause();
                    audio.currentTime = 0;
                    audio.volume = originalVolume;
                }).catch(() => {
                    // 忽略错误
                });
            }
        });
    }
}

// 创建全局音频系统实例
window.mp3AudioSystem = new MP3AudioSystem();

// 在用户首次交互时启用音频
document.addEventListener('click', function enableAudio() {
    if (window.mp3AudioSystem) {
        mp3AudioSystem.enableAudioAfterInteraction();
    }
    document.removeEventListener('click', enableAudio);
}, { once: true });